package com.example.demo_1106.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo_1106.entity.Userinfo;
import com.example.demo_1106.service.UserinfoService;
import com.example.demo_1106.mapper.UserinfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author kongke
* @description 针对表【userinfo(用户表)】的数据库操作Service实现
* @createDate 2023-07-23 10:09:06
*/
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo>
    implements UserinfoService{

    @Autowired
    UserinfoMapper userinfoMapper;

    /**
     * 获取用户
     *
     * @param account 账户
     * @return {@link List}<{@link Userinfo}>
     */
    @Override
    public List<Userinfo> getUser(String account) {
        List<Userinfo> userList;
        if ("".equals(account) || account == null ){
            userList = userinfoMapper.selectList(null);
        }else {
            userList = userinfoMapper.selectList(new QueryWrapper<Userinfo>().eq("userLoginName",account));
        }
        return userList;
    }

    /**
     * 删除用户
     *
     * @param account 账户
     * @return boolean
     */
    @Override
    public boolean delUser(String account) {
        int del = userinfoMapper.delete(new QueryWrapper<Userinfo>().eq("userLoginName", account));
        return del != 0;
    }

    /**
     * 添加用户
     *
     * @param user 用户
     * @return boolean
     */
    @Override
    public boolean addUser(Userinfo user){
        if (user != null) {
            int insert = userinfoMapper.insert(user);
            return insert !=0;
        }
        return false;
    }

    /**
     * 更新用户
     *
     * @param user    用户
     * @return boolean
     */
    @Override
    public boolean updateUser(Userinfo user){
        int update = userinfoMapper.update(user, new UpdateWrapper<Userinfo>().eq("userLoginName", user.getUserloginname()));
        return update != 0;
    }
}




